import { defineStore } from "pinia";
let modules = import.meta.glob('./../views/**/*.vue')
import { getAuth} from '@/api/index'
import router from "@/router";
export const userStore = defineStore('user',{
    state(){
        return {
            username:null as null|string,
            avatar:null  as null|string,
            status:false,
            expiration:0,
            menus:[] as any[]
        }
    },
    actions:{
        async getmenus(){
            try {
                // 动态路由
                // let {data:{data}} = await getResources();
                let {data} = await getAuth();
                // console.log(data);
                let menus:any[] = [];
                if(data.length>0){
                    let res = data.filter((item:any)=>item.route_enabled == 1)
                    res.forEach((element:any) => {
                        // 添加菜单信息
                        menus.push({
                            id:element.id,
                            pid:element.pid,
                            type:element.type,
                            index:element.menu_path,
                            icon:element.menu_icon,
                            menuname:element.menu_name,
                            path:element.route_path,
                            name:element.route_name,
                            component:element.route_component?modules[`../views/${element.route_component}.vue`]:null,
                            meta:{
                                title:element.route_meta_title,
                                require_auth:element.route_meta_require_auth,
                                keep_alive:element.route_keep_alive == 1?true:false,
                            }
                        })
                    });
                    // console.log('auth',menus,data);
                    this.menus = this.buildTree(menus,1) as any[]
                }
              } catch (error) {
                return error
              }
            
        },
        buildTree(items:any[], pid = 0) {
            //查找pid子节点
            let pitems = items.filter(s => s.pid == pid)
            if (!pitems || pitems.length <= 0)
            return null
            //递归
            pitems.forEach(item => {
                const res = this.buildTree(items, item.id)
                if (res && res.length > 0){
                    item['children'] = []
                    for(let i =0;i<res.length;i++){
                        if(res[i].type == 1){
                            item.children.push(res[i])
                        }
                        else if(res[i].type == 2){
                            item.meta[res[i].index] =true
                        }
                    }
                }
            })
            // console.log(pitems)
            return pitems
        }
    },
    // menus:[] as any[]
    persist:{
        key:'user',
        storage:localStorage,
        paths:['username','avatar','status','expiration']
    }
});

export const systemStore = defineStore('system',{
    state() {
        return {
            expandbool:false
        }
    },
    persist:true
})

interface tabsType {
    title:string
    path:string
    name:string
    keepalive:boolean
}

export const tabsStore = defineStore('tabs',{
    state(){
        return {
            tabs:[{
                title:'主页',
                path:'/home',
                name:'home',
                keepalive:true
            }] as tabsType[],
            index:''
        }
    },
    actions:{
        toRouter(to:string){
            let arr = this.tabs.filter(e=>{
                return e.path == to;
            })
            if(arr.length>0){
                return
            }
            const routes = router.getRoutes();
            const route = routes.filter(e=>{
                return e.path == to
            })
            if(route.length == 1){
                this.tabs.push({
                    title:route[0].meta.title as string,
                    path:route[0].path as string,
                    name:route[0].name as string,
                    keepalive:route[0].meta.keep_alive as boolean
                })
            }
        },
        close(to:string){
            this.tabs.filter((e,index)=>{
                if(to ==  e.path){
                    if(this.tabs.length>1&&(this.tabs.length-1 == index)){
                        this.tabs.splice(index,1)
                        router.push(this.tabs[this.tabs.length-1].path)
                    }else if(this.tabs.length==1){
                        router.push('/home')
                    }else{
                        this.tabs.splice(index,1)
                    }
                }
            })
        },
       
    },
    persist:true
})